// pages/project/project.js
const app = getApp()
var api = require("../../utils/api.js")
var util = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '项目', //导航栏 中间的标题
      hidden: 0
    },
    height: app.globalData.height * 2 + 20, // 此页面 页面内容距最顶部的距离
    projectId: 0,
    pages: 1,
    content: {},
    locationList: [],
    blogList: [],
    reportList: [],
    teamGroup: {},
    partnerList:[]
  },
  onSelectPartner(e) {
    let id = e.currentTarget.dataset.item.id
    wx.navigateTo({
      url: '../partner/partner?id=' + id
    })
  },
  onSelectLocation(e) {
    let id = e.currentTarget.dataset.item.locationId
    wx.navigateTo({
      url: '../country/index?id=' + id
    })
  },
  onSelectBlog(e) {
    let url = e.currentTarget.dataset.item.url
    wx.downloadFile({
      url: url,
      success: function (res) {
        var filePath = res.tempFilePath;
        console.log(filePath);
        wx.openDocument({
          filePath: filePath,
          fileType: 'pdf',
          success: function (res) {
            console.log(res);
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        })
      },
      fail: function (res) {
        console.log('');
        wx.showToast({
          title: '文件下载失败,请重试',
          icon: 'none',
          duration: 2000
        })
      },
      complete: function (res) { },
    })
    console.log('故事:', url)
  },
  clickTeamName(e) {
    console.log(e.currentTarget.dataset);
    let name = e.currentTarget.dataset.item;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../team/team?id=' + id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      projectId: options.id || 2
    });
    var data = {
      projectId: options.id || 2,
    };
    var url = app.u.url + app.u.project.GetProject;
    api.requestUrl(data, url).then(res => {
      console.log(res.data)
      let strs = res.data.info.teamName.split(',')
      let ids = res.data.info.tId.split(',')
      let teamGroup = {
        ids: ids,
        strs: strs
      }
      that.setData({
        content: res.data.info,
        blogList: res.data.gsList,
        partnerList: res.data.sponsorList,
        reportList: res.data.bgList,
        teamGroup: teamGroup
      });
    });
    this.onSelect();
  },
  onSelect() {
    var that = this
    var data = {
      projectId: that.data.projectId,
      currentPage: that.data.pages
    }
    var url = app.u.url + app.u.project.NextLocation;
    api.requestUrl(data, url).then(res => {
      console.log(res.data)
      that.setData({
        locationList: res.data
      });
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})